<template>
  <v-md-editor
      v-model="infoData.data.text"
      :disabled-menus="[]"
      height="800px"
      @upload-image="handleUploadImage"/>
  <el-button @click=submit type="primary" style="float: right">提交</el-button>
</template>
<script lang="ts" setup>
import {reactive} from "vue";
import {addRequest, imageUploadRequest} from "@/api/request/blog/add";
import {closeLoading} from "@/utils/elementUtils";
import {jsonJudgeNotDefined} from "@/utils/common";
//data
const infoData = reactive({
  data: {
    text: ''
  }
})
//图片上传
const handleUploadImage = async (event: object, insertImage: Function, files: any[]) => {
  const fileName = files[0].name
  let imageUrl = ''
  await imageUploadRequest(files[0]).then((res) => {
    closeLoading()
    let data = jsonJudgeNotDefined(res, 'res.data.data')
    imageUrl = data.url
  })
  insertImage({
    url: imageUrl,
    desc: fileName
  })
}
//提交
const submit = () => {
  if ((infoData.data.text ?? '') !== '') {
    let data = infoData.data.text
    addRequest(data).then(() => {
      closeLoading()
    })
  }
}

</script>
<style lang="scss" scoped>

</style>